package com.fzh.wangxiao.web.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzh.wangxiao.admin.entity.User;
import com.fzh.wangxiao.admin.service.UserService;
import com.fzh.wangxiao.utils.Result;
import com.fzh.wangxiao.web.dispatch.entity.TBizDispatch;
import com.fzh.wangxiao.web.dispatch.service.TBizDispatchRecordService;
import com.fzh.wangxiao.web.dispatch.service.TBizDispatchService;
import com.fzh.wangxiao.web.member.entity.TBizMember;
import com.fzh.wangxiao.web.member.mapper.TBizMemberMapper;
import com.fzh.wangxiao.web.member.service.TBizMemberService;
import com.fzh.wangxiao.web.req.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-01-05
 */
@Service
public class TBizMemberServiceImpl extends ServiceImpl<TBizMemberMapper, TBizMember> implements TBizMemberService {


    @Autowired
    private TBizDispatchService dispatchService;
    @Autowired
    private UserService userService;

    @Autowired
    private TBizDispatchRecordService dispatchRecordService;

    @Override
    public Result list(MemberListRequest request) {
        Result result = new Result();
        QueryWrapper<TBizMember> wrapper = new QueryWrapper();
//       wrapper.eq("status","T");
        Page<TBizMember> pages = new Page<>(request.getPageNo(),request.getPageSize());
        IPage<TBizMember> mapIPage = baseMapper.selectPage(pages, wrapper);
        result.setData(mapIPage.getRecords());
        return result;
    }

    @Override
    public Result assign(AssignRequest request) {
        Result result = new Result();
        TBizDispatch tBizDispatch = new TBizDispatch();
        tBizDispatch.setStatus(0);
        tBizDispatch.setMemberId(request.getMemberId());
        tBizDispatch.setUserId(request.getUserId());
        dispatchService.add(tBizDispatch);
        //修改状态

        TBizMember tBizMember = new TBizMember();
        tBizMember.setId(request.getId());
        tBizMember.setStatus(1);
        baseMapper.updateById(tBizMember);
        return result;
    }


    @Override
    public Result followList(FollowListRequest request) {

        return dispatchRecordService.followByMid(request);
    }


    @Override
    public Result info(MemberRequest request) {
        Result result = new Result();

        return result;
    }

    @Override
    public Result findUsersByRid(AssignRequest request) {
        List<User> list =  userService.findUsersByRid(2);
        return new Result().setData(list);
    }



    @Override
    public String getMemberName(String userId) {
        TBizMember member =  baseMapper.selectById(userId);
        return member!=null?member.getNick():"";
    }
}
